import 'package:async/async.dart';
import 'package:boss/models/coupon.dart';
import 'package:boss/ui/page/marketing/coupon/couponDetail/coupon_detail_page.dart';
import 'package:boss/ui/page/marketing/coupon/couponList/modal/coupon_share_modal.dart';
import 'package:boss/ui/page/marketing/coupon/couponList/provider/coupon_list_provider.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

import 'coupon_item.dart';

typedef QueryCouponList = Future<Result<List<Coupon>>> Function();

class CouponList extends StatefulWidget {
  final List<Coupon> couponList;
  final QueryCouponList refreshTask;
  final int type;

  const CouponList(this.couponList, this.refreshTask, this.type);

  @override
  _CouponListState createState() => _CouponListState();
}

class _CouponListState extends State<CouponList> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return LoaderPullRefresh<Coupon>(
      refreshTask: widget.refreshTask,
      builder: (_) => widget.couponList.length == 0
          ? EmptyWidget()
          : ListView.builder(
        padding: EdgeInsets.symmetric(
          horizontal: 30.w,
          vertical: 15.w,
        ),
        itemBuilder: (context, index) => CouponItem(
          widget.couponList[index],
          widget.type,
          onSendCouponTap: () => showCouponShareModal(context, widget.couponList[index]),
          onTap: () async {
            await Navigator.pushNamed(context, CouponDetailPage.pageName, arguments: {
              'coupon': widget.couponList[index],
            });

            final provider = Provider.of<CouponListProvider>(context, listen: false);
            provider.queryInvalidCouponList();
            provider.queryValidCouponList();
          },
        ),
        itemCount: widget.couponList.length,
        itemExtent: 200.w,
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
